﻿/* 2012/10/4 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cosmos.Windows.Forms
{
    /// <summary>
    /// Represents a split container which contains tabs.
    /// </summary>
    partial class TabSplitContainer : SplitContainer
    {
        public TabSplitContainer()
        {
            InitializeComponent();
        }

        public TabSplitContainer(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// Joins all the tabs in current container, and replace the container by a tab control.
        /// </summary>
        public void Join()
        {
            Control control1 = Panel1.Controls[0];
            Control control2 = Panel2.Controls[0];

            if (control1 is TabSplitContainer)
            {
                ((TabSplitContainer)control1).Join();
                control1 = Panel1.Controls[0];
            }

            if (control2 is TabSplitContainer)
            {
                ((TabSplitContainer)control2).Join();
                control2 = Panel2.Controls[0];
            }

            SplitTabControl subTabControl1 = (SplitTabControl)control1;
            SplitTabControl subTabControl2 = (SplitTabControl)control2;

            SplitTabControl tabControl = new SplitTabControl();
            ControlManager.MoveTabPage(subTabControl1.TabPages, tabControl.TabPages);
            ControlManager.MoveTabPage(subTabControl2.TabPages, tabControl.TabPages);

            // Releases sub SplitTabControls.
            Panel1.Controls.RemoveAt(0);
            Panel2.Controls.RemoveAt(0);

            Control parent = this.Parent;
            tabControl.Dock = DockStyle.Fill;
            parent.SuspendLayout();

            parent.Controls.Remove(this);
            parent.Controls.Add(tabControl);
            parent.ResumeLayout();
        }
    }
}
